package dmt16.TelemetrySuit.UI.Tiles
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.ForceSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ISensorType;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorEvent;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Assets.ForceTileGFX;
	import dmt16.TelemetrySuit.UI.Colours;
	import dmt16.TelemetrySuit.UI.DetailView;
	import dmt16.TelemetrySuit.UI.PlotTools.Graph;
	import dmt16.TelemetrySuit.UI.PlotTools.GraphLine;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class ForceTile extends TileBase
	{
		public function ForceTile(sensorLeft:ForceSensor,sensorRight:ForceSensor)
		{
			_sensorLeft=sensorLeft;
			_sensorRight=sensorRight;
			
			_sensorRight.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onSensorData);
			
			_detailView=new Graph();
			detailContent=_detailView;
			
			_leftLine=new GraphLine(_sensorLeft.data);
			_leftLine.lineStyle.color=Colours.C1;
			_leftLine.lineStyle.alpha=0.6;
			_rightLine=new GraphLine(_sensorRight.data);
			_rightLine.lineStyle.color=Colours.C2;
			_rightLine.lineStyle.alpha=0.6;
			_leftAvg=new GraphLine(_sensorLeft.movingAverage,_sensorLeft.xdata);
			_leftAvg.lineStyle.color=Colours.C1;
			_leftAvg.lineStyle.thickness=1.5;
			_rightAvg=new GraphLine(_sensorRight.movingAverage,_sensorRight.xdata);
			_rightAvg.lineStyle.color=Colours.C2;
			_rightAvg.lineStyle.thickness=1.5;
			
			_detailView.addLine(_leftLine);
			_detailView.addLine(_rightLine);
			_detailView.addLine(_leftAvg);
			_detailView.addLine(_rightAvg);
			
			_forceTile=new ForceTileGFX();
			_leftFootGraphic=MovieClip(MovieClip(_forceTile.getChildAt(0)).getChildAt(0)).getChildAt(0);
			_rightFootGraphic=MovieClip(MovieClip(_forceTile.getChildAt(0)).getChildAt(0)).getChildAt(1);
			addChild(_forceTile);
			_forceTile.width=WIDTH;
			_forceTile.height=HEIGHT;
						
			_pointer=DisplayObjectContainer(_forceTile.getChildAt(0)).getChildAt(1);
			
			_cadenceText=new TextField();
			addChild(_cadenceText);
			_cadenceText.x=0;
			_cadenceText.y=HEIGHT/4-15*HEIGHT/300;
			_cadenceText.width=WIDTH;
			_cadenceText.height=HEIGHT/2;
			_cadenceText.defaultTextFormat=_textFormat;
			_cadenceText.embedFonts=true;
			_cadenceText.selectable=false;
			
			var balanceText:TextField=new TextField();
			addChild(balanceText);
			balanceText.x=0;
			balanceText.y=HEIGHT-120*HEIGHT/300;
			balanceText.width=WIDTH;
			balanceText.height=HEIGHT/2;
			balanceText.defaultTextFormat=_textFormat2;
			balanceText.embedFonts=true;
			balanceText.selectable=false;
			balanceText.text="balance";
		}
		
		private var _forceTile:ForceTileGFX;
		private var _leftFootGraphic:DisplayObject;
		private var _rightFootGraphic:DisplayObject;
		
		private var _detailView:Graph;
		private var _leftLine:GraphLine;
		private var _rightLine:GraphLine;
		private var _leftAvg:GraphLine;
		private var _rightAvg:GraphLine;
		private var _pointer:DisplayObject;
		
		private var _cadenceText:TextField;
		
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=55*HEIGHT/300;
			_textFormat.align=TextFormatAlign.CENTER;
			_textFormat.color=Colours.FG;
		}
		
		private static var _textFormat2:TextFormat;
		{
			_textFormat2=new TextFormat();
			_textFormat2.font=Fonts.DSDigitalBold;
			_textFormat2.size=30*HEIGHT/300;
			_textFormat2.align=TextFormatAlign.CENTER;
			_textFormat2.color=Colours.FG;
		}
		
		public override function drawDetailContent():void{
			_detailView.fullRedraw();
		}
		
		private var _n:Number=0;
		
		private function _onSensorData(event:SensorEvent):void{
			_leftFootGraphic.alpha=(_sensorLeft.data[_sensorLeft.data.length-1]/1024)*0.8+0.2;
			_rightFootGraphic.alpha=(_sensorRight.data[_sensorRight.data.length-1]/1024)*0.8+0.2;
			_cadenceText.text="Cadence\r"+(Math.round((_sensorLeft.cadence+_sensorRight.cadence)/2).toString());
			_cadenceText.setTextFormat(_textFormat2,0,7);
			if(_sensorLeft.movingAverage.length>0&&_sensorRight.movingAverage.length>0){
				var leftavg:Number=_sensorLeft.movingAverage[_sensorLeft.movingAverage.length-1];
				var rightavg:Number=_sensorRight.movingAverage[_sensorRight.movingAverage.length-1];
				var balance:Number=(rightavg-leftavg)/1023;
				_pointer.x=((balance)*80+80)*HEIGHT/250;
			}
		}
		
		public override function get sensor():ISensorType
		{
			throw new Error("ForceSensor does not use TileBase.sensor, use ForceSensor.sensorLeft and ForceSensor.sensorRight instead");
		}
		
		public override function get title():String{
			return "Force";
		}
		
		private var _sensorLeft:ForceSensor;
		public function get sensorLeft():ForceSensor
		{
			return _sensorLeft;
		}
		
		private var _sensorRight:ForceSensor;
		public function get sensorRight():ForceSensor
		{
			return _sensorRight;
		}
	}
}